package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysOperLogDTO;
import com.andy.blog.admin.service.SysOperLogBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.dal.vo.SysOperLogVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperLogController {

    @Resource
    private SysOperLogBusService sysOperLogBusService;

    /**
     * 分页查询
     */
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SysOperLogDTO>> list(SysOperLogVO sysOperLogVO) {
        return GeneralResponseResult.success(sysOperLogBusService.selectListByPage(sysOperLogVO));
    }

    /**
     * 导出
     */
    @Log(title = "操作日志", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogVO sysOperLogVO) {
        List<SysOperLogDTO> list = sysOperLogBusService.selectListByCondition(sysOperLogVO);
        EasyExcelUtils.writeStyleExcel(response, list, "操作日志", SysOperLogDTO.class);
    }

    /**
     * 删除
     */
    @Log(title = "操作日志", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{ids}")
    public GeneralResponseResult<Void> remove(@PathVariable List<Long> ids) {
        sysOperLogBusService.deleteByIds(ids);
        return GeneralResponseResult.success();
    }

    /**
     * 清空
     */
    @Log(title = "操作日志", businessType = BusinessTypeEnum.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public GeneralResponseResult<Void> clean() {
        sysOperLogBusService.cleanOperLog();
        return GeneralResponseResult.success();
    }
}
